package com.roy.springboot.security.oauth2.config;

import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created by elloxxn on 12/14/2017.
 */
public class CustomTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
            Map<String, Object> additionalInfo = new HashMap<>();
            CustomerUserDetails userDetails = (CustomerUserDetails)oAuth2Authentication.getPrincipal();
            additionalInfo.put("organization", userDetails.getCorpid());

            oAuth2AccessToken.getAdditionalInformation().putAll(additionalInfo);
            //((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(additionalInfo);
            return oAuth2AccessToken;
        }

    private String randomAlphabetic(int i) {
        return new Random(i).nextInt(9999) + "";
    }

}
